package com.atguigu.gmall.payment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.atguigu.gmall.model.enums.PaymentType;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.payment.config.AlipayConfig;
import com.atguigu.gmall.payment.mapper.OrderInfoMapper;
import com.atguigu.gmall.payment.service.AlipayService;
import com.atguigu.gmall.payment.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AlipayServiceImpl implements AlipayService {

    @Autowired
    private AlipayClient alipayClient;
    @Autowired
    private PaymentService paymentService;

    //SDK 远程调用 支付宝公司
    //统一收单下单并支付接口
    @Override
    public String submit(Long orderId) {

        //1.生成财务系统需要的"支付表" 和钱相关的表
        PaymentInfo paymentInfo = paymentService.insert(orderId, PaymentType.ALIPAY);
        //注意幂等性问题
        //1.实例化客户端 支付宝客户端
        //公共参数 封装在了配置文件
        //2.接口 统一收单下单并支持接口 alipay/trade.page.pay
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        //异步回调:支付宝->网关->支付微服务
        request.setNotifyUrl(AlipayConfig.notify_payment_url);
        //同步回调:支付宝->网关->支付微服务->重定向到浏览器->再次发出请求->网关->页面微服务显示支付成功
        request.setReturnUrl(AlipayConfig.return_payment_url);
        //请求参数
        JSONObject bizContent = new JSONObject();  //四个必选值
        bizContent.put("out_trade_no", paymentInfo.getOutTradeNo());//交易号: 尚品汇是:ATGUIGU+时间戳+随机数
        bizContent.put("total_amount", paymentInfo.getTotalAmount());//总金额
        bizContent.put("subject", paymentInfo.getSubject());
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");//商品码-快速销售,目前只支持FAST_INSTANT_TRADE_PAY
        bizContent.put("qr_pay_mode", "2");//支付模式 默认1
        request.setBizContent(bizContent.toString());
        //3.发出此请求
        AlipayTradePagePayResponse alipayTradePagePayResponse = null;
        try {
            alipayTradePagePayResponse = alipayClient.pageExecute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if (alipayTradePagePayResponse.isSuccess()) {
            System.out.println("调用成功!!!");
            return alipayTradePagePayResponse.getBody();
        } else {
            System.out.println("响应失败!!!");
            return null;
        }
    }

    //退款
    @Override
    public void refund(String outTradeNo) {
        //1.查询支付表
        PaymentInfo paymentInfo = paymentService.selectPayment(outTradeNo);
        AlipayTradeRefundRequest alipayTradeRefundRequest = new AlipayTradeRefundRequest();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("out_trade_no", paymentInfo.getOutTradeNo());
        jsonObject.put("refund_amount", paymentInfo.getTotalAmount());
        alipayTradeRefundRequest.setBizContent(jsonObject.toString());
        try {
            AlipayTradeRefundResponse alipayTradeRefundResponse = alipayClient.pageExecute(alipayTradeRefundRequest);
            if (alipayTradeRefundResponse.isSuccess()){
                System.out.println("退款成功");
            }else {
                System.out.println("退款失败");
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
    }
}
